from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import api_view
from admincharge.utils.jwt import MyJWTAuthentication
from utils.pagination import PetMessagePagination
from admincharge.models import FosterPet,PetOwner
from PetNetadmin.serializers import FosterMessageSerializer
from rest_framework import serializers


# 获取所有信息并且分页
class GetAllFosterList(APIView):
    authentication_classes = [MyJWTAuthentication]
    # 展示寄养数据
    def get(self, request):
        keyword = request.GET.get('keyword')
        if keyword:
            Foster_all_list = FosterPet.objects.filter(pet_foster_master_phone__contains=keyword)
        else:
            Foster_all_list = FosterPet.objects.all()
        paginator = PetMessagePagination()
        result_page = paginator.paginate_queryset(Foster_all_list, request)
        serializer = FosterMessageSerializer(instance=result_page, many=True)
        result = {
            'data': serializer.data,
            'code': 2000,
            'msg': '获取成功'
        }
        return paginator.get_paginated_response(result)
    # 添加寄养数据
    def post(self, request):
        get_message = request.data
        print(get_message)
        data = {
            'pet_foster_name': get_message['pet_foster_name'],
            'pet_foster_master': get_message['pet_foster_master'],
            'pet_foster_master_phone': get_message['pet_foster_master_phone'],
            'pet_foster_master_IDcard': get_message['pet_foster_master_IDcard'],
            'foster_start_time': get_message['foster_start_time'],
            'foster_end_time': get_message['foster_end_time'],
            'foster_unit_price': int(get_message['foster_unit_price']),
            'is_state': get_message['is_state'],
            'is_checkout': get_message['is_checkout'],
            'description': get_message['description']
        }
        serializer = FosterMessageSerializer(data=data, partial=True)
        serializer.is_valid(raise_exception=True)
        # except serializers.ValidationError as e:
        #     result = {
        #         'code': 4000,
        #         'msg': '请先录入顾客信息再来进行寄宠登记',
        #         'details': str(e)
        #     }
        #     return Response(data=result, status=status.HTTP_400_BAD_REQUEST)

        serializer.save()
        result = {
            'code': 2000,
            'msg': '添加成功'
        }
        return Response(status=status.HTTP_200_OK, data=result)

# 编辑数据
@api_view(['POST'])
def editFosterMsg(request):
    get_message = request.data
    try:
        pet_owner_instance_name = PetOwner.objects.get(pet=get_message['pet_foster_name'])
        pet_owner_instance_master = PetOwner.objects.get(ownername=get_message['pet_foster_master'])
    except PetOwner.DoesNotExist:
        result = {
            'code': 4001,
            'msg': '无效的宠物主人姓名或宠物名字'
        }
        return Response(data=result, status=status.HTTP_400_BAD_REQUEST)
    data = {
        'pet_foster_name': pet_owner_instance_name.id,
        'pet_foster_master': pet_owner_instance_master.id,
        'pet_foster_master_phone': get_message['pet_foster_master_phone'],
        'pet_foster_master_IDcard': get_message['pet_foster_master_IDcard'],
        'foster_start_time': get_message['foster_start_time'],
        'foster_end_time': get_message['foster_end_time'],
        'foster_unit_price': int(get_message['foster_unit_price']),
        'is_state': get_message['is_state'],
        'is_checkout': get_message['is_checkout'],
        'description': get_message['description']
    }
    try:
        id = request.data['id']
        updateMsg = FosterPet.objects.get(id=id)
        print(updateMsg)
        serializer = FosterMessageSerializer(instance=updateMsg, data=data)
        if serializer.is_valid(raise_exception=True):
            serializer.save()
            result = {
                'code': 2000,
                'msg': '上传保存成功'
            }
        return Response(result)
    except PetOwner.DoesNotExist:
        return Response({"msg": '当前没有该记录可以修改'}, status=status.HTTP_404_NOT_FOUND)

# 删除单个数据
@api_view(['POST'])
def deleteOneFostermsg(request, id):
    try:
        FosterMsg = FosterPet.objects.get(id=id)
        FosterMsg.delete()
        result = {
            'code': 2000,
            'msg': '删除成功'
        }
        return Response(data=result, status=status.HTTP_200_OK)
    except PetOwner.DoesNotExist:
        return Response({"msg": '当前没有该记录可以删除'}, status=status.HTTP_404_NOT_FOUND)
# 删除多个数据
@api_view(['POST'])
def deleteManyFostermsg(request):
    try:
        ids = request.data['Ids']
        # print(ids)
        for id in ids:
            custom_message = FosterPet.objects.get(id=id)
            custom_message.delete()
        result = {
            'code': 2000,
            'msg': '删除成功'
        }
        return Response(data=result, status=status.HTTP_200_OK)
    except PetOwner.DoesNotExist:
        return Response({"msg": '当前没有该记录可以删除'}, status=status.HTTP_404_NOT_FOUND)